package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.dto.TemporarilyDTO;
import com.sc.nft.entity.UserEcologyCastUpperLimit;
import com.sc.nft.entity.UserEcologyCollectionRecords;
import com.sc.nft.entity.vo.ecology.AdminUserEcologyReforgeUpperLimitDetailsVO;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserEcologyReforgeUpperLimit;
import com.sc.nft.mapper.UserEcologyReforgeUpperLimitMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 生态-用户重铸上限表(sc_user_ecology_reforge_upper_limit)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-07 09:04:10
 */
@Slf4j
@Repository
public class UserEcologyReforgeUpperLimitDao extends ServiceImpl<UserEcologyReforgeUpperLimitMapper, UserEcologyReforgeUpperLimit> {

    /**
     * 获取用户藏品的重铸上限
     *
     * @param userId
     * @param collectionId
     * @return
     */
    public UserEcologyReforgeUpperLimit getOneByUserIdAndCollectionId(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserEcologyReforgeUpperLimit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEcologyReforgeUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyReforgeUpperLimit::getCollectionId, collectionId);
        wrapper.last("order by id  desc limit 1");
        return getOne(wrapper);
    }

    /**
     * 用户重铸上限详情
     *
     * @param userTel
     * @return
     */
    public List<AdminUserEcologyReforgeUpperLimitDetailsVO> userEcologyReforgeUpperLimitDetails(String userTel) {
        return baseMapper.userEcologyReforgeUpperLimitDetails(userTel);
    }

    /**
     * 增加用户重铸上限
     *
     * @param userId
     * @param collectionId
     * @param count
     * @return
     */
    public Boolean addCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count) {
        LambdaUpdateWrapper<UserEcologyReforgeUpperLimit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyReforgeUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyReforgeUpperLimit::getCollectionId, collectionId);
        wrapper.setSql("usable_count = usable_count + " + count);
        return update(wrapper);
    }

    public Boolean addNewCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, Integer usableCount) {
        LambdaUpdateWrapper<UserEcologyReforgeUpperLimit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyReforgeUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyReforgeUpperLimit::getCollectionId, collectionId);
        wrapper.eq(UserEcologyReforgeUpperLimit::getUsableCount,usableCount);
        wrapper.setSql("usable_count = usable_count + " + count);
        return update(wrapper);
    }

    /**
     * 减少用户重铸上限
     *
     * @param userId
     * @param collectionId
     * @param count
     * @return
     */
    public boolean subCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count) {
        return baseMapper.subCountByUserIdAndCollectionId(userId, collectionId, count);
    }

    /**
     * 减少用户重铸上限
     *
     * @param userId
     * @param collectionId
     * @param count
     * @return
     */
    public boolean subNewCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, Integer usableCount) {
        if (count.intValue() > usableCount){
            count = usableCount;
        }
        return baseMapper.subNewCountByUserIdAndCollectionId(userId, collectionId, count, usableCount);
    }


    /**
     * 批量入库
     *
     * @param list
     * @return
     */
    public Integer saveList(List<UserEcologyReforgeUpperLimit> list) {
        return baseMapper.saveList(list);
    }

    /**
     * 批量入库
     *
     * @param list
     * @return
     */
    public Integer saveTemporarilyDTOList(List<TemporarilyDTO> list) {
        return baseMapper.saveTemporarilyDTOList(list);
    }
}